function paddingZero(val) {
	return val < 10 ? (`0${ val }`) : val;
}

function getDate(value) {
	// handle undefined null
	if (!value) return null;
	if (typeof value == "object") {
		if (value.getFullYear) {
			// handle date
			var date = value;
		} else if (value.time) {
			// handle {time:58566888,......}
			date = new Date(value.time);
		} else {
			return null;
		}
	} else if (typeof value == "number") {
		// handle 45345345
		date = new Date(value);
	} else {
		return null;
	}
	return date;
}

function format(value, str, unPaddingZero) {
	let date = getDate(value);
	if (!date) return '';
	str = str || "yyyy-MM-dd";
	return str.replace("yyyy", date.getFullYear())
		.replace("MM", unPaddingZero ? (date.getMonth() + 1) : paddingZero(date.getMonth() + 1))
		.replace("dd", unPaddingZero ? date.getDate() : paddingZero(date.getDate()))
		.replace("hh", unPaddingZero ? date.getHours() : paddingZero(date.getHours()))
		.replace("mm", unPaddingZero ? date.getMinutes() : paddingZero(date.getMinutes()))
		.replace("ss", unPaddingZero ? date.getSeconds() : paddingZero(date.getSeconds()));
}
var dateUtil = {
	format,
	addYears(date, value) {
		date = new Date(date.getTime());
		date.setFullYear(date.getFullYear() + value);
		return date;
	},
	addDays(date, value) {
		date = new Date(date.getTime());
		date.setDate(date.getDate() + value);
		return date;
	},
	getQuarterStart(date) {
		const nowMonth = date.getMonth();
		const nowYear = date.getYear();
		let m = 0;
		if (nowMonth < 3) { //012
			m = 0;
		} else if (2 < nowMonth && nowMonth < 6) { //345
			m = 3;
		} else if (5 < nowMonth && nowMonth < 8) { //678
			m = 6;
		} else if (nowMonth > 8) { //9 10 11
			m = 9;
		}
		let quarterStart = new Date(nowYear, m, 1)
		return quarterStart;
	},
	addMonths(date, value) {
	  date = new Date(date.getTime());
	  date.setMonth(date.getMonth() + value);
	  return date;
	},
	addSeconds(date, value) {
	  date = new Date(date.getTime());
	  date.setSeconds(date.getSeconds() + value);
	  return date;
	},
	// // 获取当前周的第一天 00:00:00（从周一开始）
	getFirstDayOfWeek(date) {
		date = new Date(date.getTime());
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		let day = date.getDay();
		if (day === 1) return date;
		if (day === 0) {
			return dateUtil.addDays(date, -6);
		}
		return dateUtil.addDays(date, -(day - 1));
	},
	getDateBegin(date) {
		date = new Date(date.getTime());
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		return date;
	},
	getDateEnd(date) {
		date = new Date(date.getTime());
		date.setHours(23);
		date.setMinutes(59);
		date.setSeconds(59);
		return date;
	},
	getMonthBegin(date) {
		date = new Date(date.getTime());
		date.setDate(1);
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		return date;
	},
	getMonthEnd(date) {
		date = new Date(date.getTime());
		date.setMonth(date.getMonth() + 1);
		date = this.getMonthBegin(date);
		date.setSeconds(-1);
		return date;
	},
	getYearBegin(date) {
		date = new Date(date.getTime());
		date.setMonth(0);
		date.setDate(1);
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		return date;
	},
	getYearEnd(date) {
		date = new Date(date.getTime());
		date.setFullYear(date.getFullYear() + 1);
		date = this.getYearBegin(date);
		date.setSeconds(-1);
		return date;
	},
	// timeDetail(seconds, lang, keys) {
	//   keys = keys || {};
	//   keys = Object.assign({
	//     day: 'day',
	//     hour: 'hour',
	//     minuteShort: 'minuteShort',
	//     secondShort: 'secondShort'
	//   }, keys);
	//   let obj = {};
	//   for (let i in keys) {
	//     obj[i] = 0;
	//   }
	//   let minutes = parseInt(seconds / 60);
	//   obj[keys.secondShort] = seconds - minutes * 60;
	//   let hours = parseInt(minutes / 60);
	//   obj[keys.minuteShort] = minutes - hours * 60;
	//   let day = parseInt(hours / 24);
	//   obj[keys.hour] = hours - day * 24;
	//   obj[keys.day] = day;
	//   let result = [];
	//   for (let i in obj) {
	//     if (obj[i] === 0) continue;
	//     result.push(obj[i] + lang[i]);
	//   }
	//   if (result.length === 0) {
	//     return 0 + lang[keys.secondShort];
	//   }
	//   return result.join("");
	// }
};
export default dateUtil;
